package view.controller;

import common.*;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;
import server.ClientConnServer;
import server.ManageSocket;
import view.*;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.concurrent.BrokenBarrierException;

public class FriendList implements Initializable {
    public ImageView selfHeadImg;
    public Label username;
    public Label userid;
    public Button addfriendbt;
    public Button groupbt;
    public Button addqlbt;
    public Button addbt;
    public Button mgroupbt;
    public Button groupbtx;
    public Button xxchangebt;
    public Button qlchangebt;
    public Button hychangebt;
    public Button refresh;
    public ImageView close;
    public ListView<Dataing> friendlistview;
    public ListView<Dataing> grouplistview;
    public ListView<Dataing> othergrouplist;
    public ListView<Dataing> newslist;
    public ListView<Dataing> groupnewslist;
    public TitledPane mygrouptd;
    public TitledPane othergrouptd;
    public TitledPane friendTitled;
    public TitledPane addfriendtd;
    public TitledPane addgrouptd;
    ObservableList<Dataing> frend = FXCollections.observableArrayList();
    ObservableList<Dataing> group = FXCollections.observableArrayList();
    ObservableList<Dataing> othergroup = FXCollections.observableArrayList();
    ObservableList<Dataing> news = FXCollections.observableArrayList();
    ObservableList<Dataing> groupnews = FXCollections.observableArrayList();

    //好友添加+群聊添加
    public void tofriendadd() throws IOException {
        Stage stage = (Stage) addfriendbt.getScene().getWindow();
        Stage newstage = new Stage();
        AddFriend rj = new AddFriend();
        rj.start(newstage);
    }
    public void toadd(ActionEvent actionEvent) throws IOException {
        Stage stage = (Stage) addbt.getScene().getWindow();
        Stage newstage = new Stage();
        AddFriend rj = new AddFriend();
        rj.start(newstage);
    }
    public void toqladd() throws IOException {
        Stage stage = (Stage) addqlbt.getScene().getWindow();
        Stage newstage = new Stage();
        AddFriend rj = new AddFriend();
        rj.start(newstage);
    }

    //创建群聊
    public void togroup(ActionEvent actionEvent) throws IOException {
        Stage stage = (Stage) groupbt.getScene().getWindow();
        CreateGroupChat rj = new CreateGroupChat();
        Stage newstage = new Stage();
        rj.start(newstage);
    }
    public void togroupx(ActionEvent actionEvent) throws IOException {
        Stage stage = (Stage) groupbtx.getScene().getWindow();
        CreateGroupChat rj = new CreateGroupChat();
        Stage newstage = new Stage();
        rj.start(newstage);
    }
    public void mtogroup(ActionEvent actionEvent) throws IOException {
        Stage stage = (Stage) mgroupbt.getScene().getWindow();
        CreateGroupChat rj = new CreateGroupChat();
        Stage newstage = new Stage();
        rj.start(newstage);
    }

    public void topersonal(MouseEvent mouseEvent) throws IOException {
        Stage stage = (Stage) selfHeadImg.getScene().getWindow();
        Stage newstage = new Stage();
        Personal rj = new Personal();
        rj.start(newstage);
    }

    //密码修改
    public void toxxchangebt(ActionEvent actionEvent) throws IOException {
        Stage stage = (Stage) xxchangebt.getScene().getWindow();
        stage.close();
        Stage newstage = new Stage();
        ChangePsw rj = new ChangePsw();
        rj.start(newstage);
    }
    public void toqlchangebt(ActionEvent actionEvent) throws IOException {
        Stage stage = (Stage) qlchangebt.getScene().getWindow();
        stage.close();
        Stage newstage = new Stage();
        ChangePsw rj = new ChangePsw();
        rj.start(newstage);
    }
    public void tohychangebt(ActionEvent actionEvent) throws IOException {
        Stage stage = (Stage) hychangebt.getScene().getWindow();
        stage.close();
        Stage newstage = new Stage();
        ChangePsw rj = new ChangePsw();
        rj.start(newstage);
    }

    public void torefresh(ActionEvent actionEvent) {
        NowUser common = new NowUser();
        User user = new User();
        user = common.getUser();
        Stage stage = (Stage) refresh.getScene().getWindow();
        stage.close();
        Stage newstage = new Stage();
        view.FriendList rj = new view.FriendList();
        try {
            rj.start(newstage);
//                        rj.Show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //头像置灰
    public WritableImage changColor(Image image) {
        int width = (int) image.getWidth();
        int height = (int) image.getHeight();
        //写入像素
        WritableImage wImage = new WritableImage(width, height);

        PixelReader pixelReader = image.getPixelReader();
        PixelWriter writer = wImage.getPixelWriter();
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                Color color = pixelReader.getColor(x, y);
                writer.setColor(x, y, color.grayscale());
            }
        }
        return wImage;
    }

    //好友列表初始化(获取好友列表)
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        User user = new User();
        user = NowUser.user;
        if (user.getName() != null) {
            username.setText(user.getName());
        }
        if (user.getId() != null) {
            userid.setText(user.getId());
        }
        if (user.getHeadimage() != null && !user.getHeadimage().equals("") && !user.getHeadimage().equals("null")) {
            Image image = new Image("file:" + user.getHeadimage());
            selfHeadImg.setImage(image);
        } else {
            Image image = new Image("view\\photo\\默认头像.jpg");
            selfHeadImg.setImage(image);
        }
        /////////////////设置下拉框
        friendTitled.setAnimated(true);//好友列表按钮收起动画
        mygrouptd.setAnimated(true);
        othergrouptd.setAnimated(true);
        addfriendtd.setAnimated(true);
        addgrouptd.setAnimated(true);

        friendTitled.setExpanded(true);//默认好友列表收起
        mygrouptd.setExpanded(false);
        othergrouptd.setExpanded(false);
        addfriendtd.setExpanded(false);
        addgrouptd.setExpanded(false);

        /////////////////

        Message msg = new Message();
        msg.setUser(user);
        msg.setType("getfriends");
        ArrayList<User> listf = new ArrayList<User>();
        try {
            new ClientConnServer().sendinfo(msg);
            NowUser.cyclicBarrier.reset();
            NowUser.cyclicBarrier.await();
            listf = NowUser.getFriendslist();
        } catch (IOException | InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }

        friendlistview.getSelectionModel().select(0);

        /**
         * 好友显示，循环列表
         */
        if (listf != null) {
            for (int i = 0; i < listf.size(); i++) {
                if (listf.get(i).getHeadimage() == null || listf.get(i).getHeadimage().equals("") || listf.get(i).getHeadimage().equals("null")) {
                    User uu = listf.get(i);
                    uu.setHeadimage("E:\\untitled2\\src\\view\\photo\\默认头像.jpg");
                    listf.set(i, uu);
                }
                Image img = new Image("file:" + listf.get(i).getHeadimage());
//                if (listf.get(i).getState() == 0) {
//                    WritableImage wimage = changColor(img);
//                    img = wimage;
//                }
                frend.add(new Dataing(listf.get(i).getName(), listf.get(i).getSign(), listf.get(i).getId(), img));
            }
        }
        friendlistview.setItems(frend);

        //获取好友列表每个单元的值
        friendlistview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Dataing>() {
            @Override
            public void changed(ObservableValue<? extends Dataing> observable, Dataing oldValue, Dataing newValue) {
            }
        });

        //好友列表的点击事件
        friendlistview.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //左键双击打开聊天室
                if (event.getClickCount() == 2 && event.getButton().equals(MouseButton.PRIMARY)) {
                    Stage stage = (Stage) friendlistview.getScene().getWindow();
                    User user = new User();
                    user.setId(friendlistview.getFocusModel().getFocusedItem().getSingleid());
                    Message tmpmsg = new Message();
                    tmpmsg.setUser(user);
                    tmpmsg.setType("getfriend");
                    try {
                        new ClientConnServer().sendinfo(tmpmsg);
                        NowUser.cyclicBarrier.reset();
                        NowUser.cyclicBarrier.await();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    //Common.setFriend(user);
                    Chat rj = new Chat();
                    FXMLLoader fxmlLoader = new FXMLLoader();
                    fxmlLoader.setLocation(getClass().getResource("../fxml/chat.fxml"));
                    try {
                        fxmlLoader.load();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ChatController chattingController = fxmlLoader.getController();
                    //System.out.println(Common.user.getUserid()+Common.friend.getUserid()+"sss");
                    ManageChat.setChatcontroller(NowUser.user.getId() + NowUser.friend.getId(), chattingController);
                    ManageChat.setChat(NowUser.user.getId() + NowUser.friend.getId(), rj);

                    System.out.println(ManageChat.getchatcontroller(NowUser.user.getId() + NowUser.friend.getId()));
                    // System.out.println(Common.user.getUserid()+Common.friend.getUserid());
                    Stage newstage = new Stage();
                    try {
                        rj.start(newstage);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                //右键单击删除好友
                if (event.getClickCount() == 1 && event.getButton().equals(MouseButton.SECONDARY)) {
                    Stage stage = (Stage) friendlistview.getScene().getWindow();
                    Stage newstage = new Stage();

                    ///
                    //System.out.println("sb"+friendlistview.getFocusModel().getFocusedItem().getSingleid());
                    User friendu = new User();
                    friendu.setId(friendlistview.getFocusModel().getFocusedItem().getSingleid());
                    Message tmpmsg = new Message();
                    tmpmsg.setUser(friendu);
                    tmpmsg.setType("getfriend");
                    try {
                        new ClientConnServer().sendinfo(tmpmsg);
                        NowUser.cyclicBarrier.reset();
                        NowUser.cyclicBarrier.await();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    //Common.setFriend(friendu);
                    DeleteFriend rj = new DeleteFriend();
                    try {
                        rj.start(newstage);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        //将listView设置为单元工厂
        friendlistview.setCellFactory(new Callback<ListView<Dataing>, ListCell<Dataing>>() {
            @Override
            public ListCell<Dataing> call(ListView<Dataing> param) {


                return new ListCell<Dataing>() {
                    @Override
                    protected void updateItem(Dataing item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!empty) {
                            setStyle("-fx-background-color:transparent");
                            HBox hBox = new HBox();
                            //导入好友列表图像
                            FXMLLoader fxmlLoader = new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("../fxml/single.fxml"));
                            //Parent root = FXMLLoader.load(getClass().getResource("../FXML/friendlist.fxml"));
                            // FriendList friendList = (FriendList) root.lookup("#friendlistview");
                            try {
                                hBox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                                System.out.println("好友列表加载错误");
                            }
                            SingleController singleController = fxmlLoader.getController();
                            hBox.setPadding(new Insets(0, 0, 0, 0));
                            singleController.setSinglename(item.singlename);
                            singleController.setSingleqm(item.singleqm);
                            singleController.setSingleheadimage(item.singleimage);
                            this.setGraphic(hBox);
                        } else {
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });

        /**
         * 群聊显示，循环列表
         * 我创建的群
         */
        Message groupmsg = new Message();
        groupmsg.setUser(user);
        Group group1 = new Group();
        group1.setuserid(NowUser.user.getId());
        groupmsg.setGroup(group1);
        groupmsg.setType("getgroups");
        ArrayList<Group> listg = new ArrayList<Group>();
        try {
            new ClientConnServer().sendinfo(groupmsg);
            NowUser.cyclicBarrier.reset();
            NowUser.cyclicBarrier.await();
            listg = NowUser.getGroupslist();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        grouplistview.getSelectionModel().select(0);
        System.out.println(listg);
        if (listg != null) {
            for (int i = 0; i < listg.size(); i++) {
                if (listg.get(i).getGroupheadimage() == null || listg.get(i).getGroupheadimage().equals("") || listg.get(i).getGroupheadimage().equals("null")) {
                    Group uu = listg.get(i);
                    uu.setGroupheadimage("E:untitled\\src\\view\\photo\\群头像.jpg");
                    listg.set(i, uu);
                }
                Image img = new Image("file:///" + listg.get(i).getGroupheadimage());
                group.add(new Dataing(listg.get(i).getGroupname(), img, listg.get(i).getGroupid()));
                System.out.println("群聊列表id" + listg.get(i).getGroupid());
            }
        }
        grouplistview.setItems(group);

        //获取群聊列表每个单元的值
        grouplistview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Dataing>() {
            @Override
            public void changed(ObservableValue<? extends Dataing> observable, Dataing oldValue, Dataing newValue) {
            }
        });

        //群聊列表的点击事件
        grouplistview.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //左键双击打开群聊聊天框
                if (event.getClickCount() == 2 && event.getButton().equals(MouseButton.PRIMARY)) {
                    Stage stage = (Stage) grouplistview.getScene().getWindow();
                    Group user = new Group();
                    //System.out.println("点击的群聊id:"+grouplistview.getFocusModel().getFocusedItem().getGroupid());
                    user.setGroupid(grouplistview.getFocusModel().getFocusedItem().getGroupid());
                    Message tmpmsg = new Message();
                    tmpmsg.setGroup(user);
                    tmpmsg.setType("getgroup");
                    try {
                        new ClientConnServer().sendinfo(tmpmsg);
                        NowUser.cyclicBarrier.reset();
                        NowUser.cyclicBarrier.await();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    GroupChat rj = new GroupChat();
                    ManageChat.setGroupchat(NowUser.user.getId() + NowUser.group.getGroupid(), rj);
                    System.out.println(NowUser.user.getId() + NowUser.group.getGroupid());
                    Stage newstage = new Stage();
                    try {
                        rj.start(newstage);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                //右键单击解散群聊
                if (event.getClickCount() == 1 && event.getButton().equals(MouseButton.SECONDARY)) {

                    Stage stage = (Stage) grouplistview.getScene().getWindow();
                    Stage newstage = new Stage();
                    ///
                    System.out.println("sb" + grouplistview.getFocusModel().getFocusedItem().getGroupid());
                    Group friendu = new Group();
                    friendu.setGroupid(grouplistview.getFocusModel().getFocusedItem().getGroupid());
                    Message tmpmsg = new Message();
                    tmpmsg.setGroup(friendu);
                    tmpmsg.setType("getgroup");
                    try {
                        new ClientConnServer().sendinfo(tmpmsg);
                        NowUser.cyclicBarrier.reset();
                        NowUser.cyclicBarrier.await();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    //Common.setGroup(friendu);
                    DissolutionGroup rj = new DissolutionGroup();
                    try {
                        rj.start(newstage);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        //将listView设置为单元工厂
        grouplistview.setCellFactory(new Callback<ListView<Dataing>, ListCell<Dataing>>() {
            @Override
            public ListCell<Dataing> call(ListView<Dataing> param) {
                return new ListCell<Dataing>() {
                    @Override
                    protected void updateItem(Dataing item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!empty) {
                            setStyle("-fx-background-color:transparent");
                            HBox hBox = new HBox();
                            //导入群聊列表图像
                            FXMLLoader fxmlLoader = new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("../fxml/single.fxml"));
                            try {
                                hBox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                                System.out.println("群聊列表加载错误");
                            }
                            SingleController singleController = fxmlLoader.getController();
                            hBox.setPadding(new Insets(0, 0, 0, 0));
                            singleController.setSinglename(item.groupname);
                            singleController.setSingleqm(item.groupid);
                            singleController.setSingleheadimage(item.groupimgv);
                            this.setGraphic(hBox);
                        } else {
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });

        /**
         * 我添加的群
         */
        Message othergroupmsg = new Message();
        othergroupmsg.setUser(user);
        Group group2 = new Group();
        group2.setuserid(NowUser.user.getId());
        othergroupmsg.setGroup(group2);
        othergroupmsg.setType("getothergroups");
        ArrayList<Group> listog = new ArrayList<Group>();
        try {
            new ClientConnServer().sendinfo(othergroupmsg);
            NowUser.cyclicBarrier.reset();
            NowUser.cyclicBarrier.await();
            listog = NowUser.getOthergroupslist();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        othergrouplist.getSelectionModel().select(0);
        //System.out.println(listog.size()+"长");
        if (listog != null) {
            for (int i = 0; i < listog.size(); i++) {
                if (listog.get(i).getGroupheadimage() == null || listog.get(i).getGroupheadimage().equals("") || listog.get(i).getGroupheadimage().equals("null")) {
                    Group uu = listog.get(i);
                    uu.setGroupheadimage("E:\\untitled\\src\\view\\photo\\群头像.jpg");
                    listog.set(i, uu);
                }
                Image img = new Image("file:///" + listog.get(i).getGroupheadimage());
                othergroup.add(new Dataing(listog.get(i).getGroupname(), img, listog.get(i).getGroupid()));
                System.out.println("我添加的群聊列表id" + listog.get(i).getGroupid());
            }
        }
        othergrouplist.setItems(othergroup);


        //获取群聊列表每个单元的值
        othergrouplist.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Dataing>() {
            @Override
            public void changed(ObservableValue<? extends Dataing> observable, Dataing oldValue, Dataing newValue) {
            }
        });


        //群聊列表的点击事件
        othergrouplist.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //左键双击打开群聊聊天框
                if (event.getClickCount() == 2 && event.getButton().equals(MouseButton.PRIMARY)) {
                    Stage stage = (Stage) othergrouplist.getScene().getWindow();
                    Group group = new Group();
                    System.out.println("点击的群聊id:" + othergrouplist.getFocusModel().getFocusedItem().getGroupid());
                    group.setGroupid(othergrouplist.getFocusModel().getFocusedItem().getGroupid());
                    Message tmpmsg = new Message();
                    tmpmsg.setGroup(group);
                    tmpmsg.setType("getgroup");
                    try {
                        new ClientConnServer().sendinfo(tmpmsg);
                        NowUser.cyclicBarrier.reset();
                        NowUser.cyclicBarrier.await();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    //Common.setGroup(group);
                    GroupChat rj = new GroupChat();
                    ManageChat.setGroupchat(NowUser.user.getId() + NowUser.group.getGroupid(), rj);
                    Stage newstage = new Stage();
                    try {
                        rj.start(newstage);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                //右键单击退出群聊
                if (event.getClickCount() == 1 && event.getButton().equals(MouseButton.SECONDARY)) {
                    Stage stage = (Stage) othergrouplist.getScene().getWindow();
                    Stage newstage = new Stage();
                    String str = othergrouplist.getFocusModel().getFocusedItem().getGroupid();
                    Group newgroup = new Group();
                    newgroup.setGroupid(str);
                    NowUser.setGroup(newgroup);
                    ExitGroup rj = new ExitGroup();
                    try {
                        rj.start(newstage);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        //将listView设置为单元工厂
        othergrouplist.setCellFactory(new Callback<ListView<Dataing>, ListCell<Dataing>>() {
            @Override
            public ListCell<Dataing> call(ListView<Dataing> param) {
                return new ListCell<Dataing>() {
                    @Override
                    protected void updateItem(Dataing item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!empty) {
                            setStyle("-fx-background-color:transparent");
                            HBox hBox = new HBox();
                            //导入好友列表图像
                            FXMLLoader fxmlLoader = new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("../fxml/single.fxml"));
                            try {
                                hBox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                                System.out.println("群聊列表加载错误");
                            }
                            SingleController singleController = fxmlLoader.getController();
                            hBox.setPadding(new Insets(0, 0, 0, 0));
                            singleController.setSinglename(item.groupname);
                            singleController.setSingleqm(item.groupid);
                            singleController.setSingleheadimage(item.groupimgv);
                            this.setGraphic(hBox);
                        } else {
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });


        /**
         * 消息列表显示
         */
        ArrayList<Information> listi = new ArrayList<Information>();
        Message immsg = new Message();
        immsg.setType("getinformations");
        immsg.setUser(user);
        try {
            new ClientConnServer().sendinfo(immsg);
            NowUser.cyclicBarrier.reset();
            NowUser.cyclicBarrier.await();
            listi = NowUser.getFriendinformationlist();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println(immsg.getUser().getId());

        if (listi != null) {
            for (int i = 0; i < listi.size(); i++) {
                if (listi.get(i).getUserimage() == null || listi.get(i).getUserimage().equals("") || listi.get(i).getUserimage().equals("null")) {
                    Information uu = listi.get(i);
                    uu.setUserimage("E:\\untitled\\src\\view\\photo\\默认头像.jpg");
                    listi.set(i, uu);
                }
                Image img = new Image("file:///" + listi.get(i).getUserimage());
                news.add(new Dataing(listi.get(i).getUsername() + " 请求添加您为好友", listi.get(i).getUsername(), listi.get(i).getUserid(), img, listi.get(i).getUserimage()));
            }
        }
        newslist.setItems(news);

        //listview点击事件
        newslist.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Dataing>() {
            @Override
            public void changed(ObservableValue<? extends Dataing> observable, Dataing oldValue, Dataing newValue) {

            }
        });

        newslist.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //左键双击打开消息验证
                if (event.getClickCount() == 2 && event.getButton().equals(MouseButton.PRIMARY)) {
                    Stage stage = (Stage) newslist.getScene().getWindow();
                    User friendu = new User();
                    friendu.setId(newslist.getFocusModel().getFocusedItem().getSingleid());
                    friendu.setName(newslist.getFocusModel().getFocusedItem().getSinglename());
                    friendu.setHeadimage(newslist.getFocusModel().getFocusedItem().getSingleimagepath());
                    NowUser.setFriend(friendu);
                    Apply rj = new Apply();
                    Stage newstage = new Stage();
                    try {
                        rj.start(newstage);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        });


        newslist.setCellFactory(new Callback<ListView<Dataing>, ListCell<Dataing>>() {
            @Override
            public ListCell<Dataing> call(ListView<Dataing> param) {
                return new ListCell<Dataing>() {
                    @Override
                    protected void updateItem(Dataing item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!empty) {
                            setStyle("-fx-background-color:transparent");
                            HBox hBox = new HBox();
                            //导入好友列表图像
                            FXMLLoader fxmlLoader = new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("../fxml/single.fxml"));
                            try {
                                hBox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                                System.out.println("消息列表加载错误");
                            }
                            SingleController singleController = fxmlLoader.getController();
                            hBox.setPadding(new Insets(0, 0, 0, 0));
                            singleController.setSinglename(item.singlename);
                            singleController.setSingleqm(item.singleid);
                            singleController.setSingleheadimage(item.singleimage);
                            this.setGraphic(hBox);
                        } else {
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });

        /**
         *群聊验证消息列表
         */
        ArrayList<Information> listig = new ArrayList<Information>();
        Message imgmsg = new Message();
        imgmsg.setType("getgroupinformations");
        imgmsg.setUser(user);
        Group gp = new Group();
        gp.setuserid(NowUser.user.getId());
        imgmsg.setGroup(gp);
        try {
            new ClientConnServer().sendinfo(imgmsg);
            NowUser.cyclicBarrier.reset();
            NowUser.cyclicBarrier.await();
            listig = NowUser.getGroupinformationlist();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.println(imgmsg.getUser().getId());

        if (listig != null) {
            for (int i = 0; i < listig.size(); i++) {
                if (listig.get(i).getGroupimage() == null || listig.get(i).getGroupimage().equals("") || listig.get(i).getGroupimage().equals("null")) {
                    Information uu = listig.get(i);
                    uu.setGroupimage("E:\\untitled\\src\\view\\photo\\群头像.jpg");
                    listig.set(i, uu);
                }
                Image img = new Image("file:///" + listig.get(i).getGroupimage());
                //System.out.println(listig.get(i).getGroupimage()+"又来啦");
                //System.out.println(listig.get(i).getUserimage());
                groupnews.add(new Dataing(listig.get(i).getUsername() + " 申请加入群聊 " + listig.get(i).getGroupid(), listig.get(i).getUsername(), listig.get(i).getGroupid(), listig.get(i).getUserid(), img, listig.get(i).getGroupimage()));
            }
        }
        groupnewslist.setItems(groupnews);

        //listview点击事件
        groupnewslist.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Dataing>() {
            @Override
            public void changed(ObservableValue<? extends Dataing> observable, Dataing oldValue, Dataing newValue) {

            }
        });

        groupnewslist.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //左键双击打开消息验证
                if (event.getClickCount() == 2 && event.getButton().equals(MouseButton.PRIMARY)) {
                    Stage stage = (Stage) groupnewslist.getScene().getWindow();
                    Group group = new Group();
                    System.out.println("群聊id=" + groupnewslist.getFocusModel().getFocusedItem().getSingleqm());
                    System.out.println("发送申请的个人id：" + groupnewslist.getFocusModel().getFocusedItem().getSingleid());
                    group.setGroupid(groupnewslist.getFocusModel().getFocusedItem().getSingleqm());
                    group.setuserid(groupnewslist.getFocusModel().getFocusedItem().getSingleid());
                    group.setGroupheadimage(groupnewslist.getFocusModel().getFocusedItem().getSingleimagepath());
                    group.setGroupname(groupnewslist.getFocusModel().getFocusedItem().getSinglename());
                    NowUser.setGroup(group);
                    GroupApplyfor rj = new GroupApplyfor();
                    Stage newstage = new Stage();
                    try {
                        rj.start(newstage);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        });


        groupnewslist.setCellFactory(new Callback<ListView<Dataing>, ListCell<Dataing>>() {
            @Override
            public ListCell<Dataing> call(ListView<Dataing> param) {
                return new ListCell<Dataing>() {
                    @Override
                    protected void updateItem(Dataing item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!empty) {
                            setStyle("-fx-background-color:transparent");
                            HBox hBox = new HBox();
                            //导入好友列表图像
                            FXMLLoader fxmlLoader = new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("../fxml/single.fxml"));
                            try {
                                hBox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                                System.out.println("消息列表加载错误");
                            }
                            SingleController singleController = fxmlLoader.getController();
                            hBox.setPadding(new Insets(0, 0, 0, 0));
                            singleController.setSinglename(item.singlename);
                            singleController.setSingleqm(item.singleqm);
                            singleController.setSingleheadimage(item.singleimage);
                            this.setGraphic(hBox);
                        } else {
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });

    }

    public void toclose(MouseEvent mouseEvent) throws IOException,BrokenBarrierException, InterruptedException {
        Message msg=new Message();
        msg.setUser(NowUser.user);
        msg.setType("close");
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
//        ManageSocket.setOos(NowUser.getUser().getId(),null);
//        ManageSocket.setOis(NowUser.getUser().getId(),null);
//        ManageSocket.setSocketHashMap(NowUser.getUser().getId(),null);
//        NowUser.cyclicBarrier.reset();
//        NowUser.cyclicBarrier.await();
        Stage stage = (Stage) close.getScene().getWindow();
        stage.close();
    }
}

//模拟数据
class Dataing {
    String singleimagepath;
    String groupimagepath;
    String singlename;
    String singleqm;
    String singleid;
    String groupname;
    String groupid;
    Image groupimgv;
    Image singleimage;

    public String getSingleimagepath() {
        return singleimagepath;
    }
    public void setSingleimagepath(String singleimagepath) {
        this.singleimagepath = singleimagepath;
    }

    public String getGroupimagepath() {
        return groupimagepath;
    }
    public void setGroupimagepath(String groupimagepath) {
        this.groupimagepath = groupimagepath;
    }

    public String getGroupname() {
        return groupname;
    }
    public void setGroupname(String groupname) {
        this.groupname = groupname;
    }

    public String getGroupid() {
        return groupid;
    }
    public void setGroupid(String groupid) {
        this.groupid = groupid;
    }

    public Image getGroupimgv() {
        return groupimgv;
    }
    public void setGroupimgv(Image groupimgv) {
        this.groupimgv = groupimgv;
    }

    public String getSingleid() {
        return singleid;
    }
    public void setSingleid(String singleid) {
        this.singleid = singleid;
    }

    public Image getSingleimage() {
        return singleimage;
    }
    public void setSingleimage(Image singleimage) {
        this.singleimage = singleimage;
    }

    public String getSinglename() {
        return singlename;
    }
    public void setSinglename(String singlename) {
        this.singlename = singlename;
    }

    public String getSingleqm() {
        return singleqm;
    }
    public void setSingleqm(String singleqm) {
        this.singleqm = singleqm;
    }

    public Dataing(String groupname, Image groupimgv, String groupid) {
        this.groupname = groupname;
        this.groupimgv = groupimgv;
        this.groupid = groupid;
    }

    //添加好友申请
    public Dataing( String singleqm,String singlename,String singleid,Image singleimage,String groupimagepath) {
        this.singlename=singlename;
        this.singleqm = singleqm;
        this.singleid = singleid;
        this.singleimage = singleimage;
        this.groupimagepath = groupimagepath;
    }

    //入群申请
    public Dataing(String groupname, String singlename, String singleqm,String singleid,Image singleimage,String groupimagepath) {
        this.singlename= singlename;
        this.groupname = groupname;
        this.singleqm = singleqm;
        this.singleid = singleid;
        this.singleimage = singleimage;
        this.groupimagepath = groupimagepath;
    }

    public Dataing(String singlename, String singleqm,String singleid,Image singleimage) {
        this.singlename= singlename;
        this.singleqm = singleqm;
        this.singleid = singleid;
        this.singleimage = singleimage;
    }

    public Dataing(String name, String id, Image imgv) {
        this.singlename = name;
        this.singleid = id;
        this.singleimage = imgv;
    }

}